package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;

import java.io.IOException;
import java.io.InputStream;

import butterknife.InjectView;
import butterknife.OnClick;

import static com.yuntongxun.ecdemo.voicechat.ui.activity.RegisterFlagActivity.FLAG;

/**
 * 注册页面3-输入年龄
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/15
 */
public class RegisterAgeActivity extends BaseChatActivity {

    public static final String AGE = "age";

    @InjectView(R.id.country_img)
    ImageView flagImg;
    @InjectView(R.id.country_name_tv)
    TextView flagNameTv;
    @InjectView(R.id.gender_img)
    ImageView genderImg;
    @InjectView(R.id.age_edt)
    EditText ageEdt;

    String gender="";

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_voice_chat_register3;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Register");
        initData();
    }

    private void initData(){
        String flagName = getIntent().getStringExtra(RegisterFlagActivity.FLAG);
        Log.e("flag", flagName + "");
        try {
            InputStream is = getAssets().open("flag/" + flagName + ".png");
            flagImg.setImageBitmap(BitmapFactory.decodeStream(is));
            flagNameTv.setText(flagName);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        gender=getIntent().getStringExtra(RegisterGenderActivity.GENDER);
        if (gender.endsWith(RegisterGenderActivity.MAN)){
            genderImg.setImageDrawable(getResources().getDrawable(R.drawable.man_check_true));
        }else{
            genderImg.setImageDrawable(getResources().getDrawable(R.drawable.woman_check_true));
        }
    }

    @OnClick(R.id.next_btn)
    public void onNextClick() {
        if (TextUtils.isEmpty(ageEdt.getText().toString().trim())){
            showToastLong(getString(R.string.please_input_age));
            return;
        }

        Intent intent=new Intent(this,RegisterInfoActivity.class);
        intent.putExtra(FLAG,flagNameTv.getText().toString().trim());
        intent.putExtra(RegisterGenderActivity.GENDER,gender);
        intent.putExtra(AGE,ageEdt.getText().toString().trim());
        startActivity(intent);
    }

    public void onEvent(PostResult postResult){
        if (EventBusTags.REGISTER_FINISH.equals(postResult.getTag())){
            finish();
        }
    }

}
